$(function () {
    var timer = null;
    checkLoginStatus();
    getShopCarData();
    checkEvent();
    checkAllEvent();
    plusEvent();
    reduceEvent();
    numKeyupEvent();
    check();
    del();
    pay();

    function changeNumById(id, num) {
        var option = {
            type: 'post',
            url: HOST + '/admin/shopCar.php',
            data: {
                postType: 'changeNum',
                id: id,
                num: num,
                user: LsyCookie.getItem('loginUserName')
            }
        };
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            request(option).then(function (res) {
                console.log(res);
            })
        }, 300);
    }

    function numKeyupEvent() {
        $('#shopCar').on('keyup', '.num', function () {
            var _vm = $(this);
            var num = _vm.val();
            var id = _vm.parents('.item').attr('data-id');
            num = num.replace(/[^0-9]+/, '').replace(/^0+/, '');
            num = num ? num : 1;
            _vm.val(num);
            calculatePrice();
            changeNumById(id, num);
        })
    }

    function reduceEvent() {
        $('#shopCar').on('click', '.reduce', function () {
            var _vm = $(this);
            var num = $(this).next();
            var id = _vm.parents('.item').attr('data-id');
            var _num = num.val() * 1;
            _num = _num > 1 ? --_num : 1;
            num.val(_num);
            calculatePrice();
            changeNumById(id, _num);
        })
    }

    function plusEvent() {
        $('#shopCar').on('click', '.plus', function () {
            var _vm = $(this);
            var id = _vm.parents('.item').attr('data-id');
            var num = _vm.prev();
            var _num = num.val() * 1 + 1;
            num.val(_num);
            calculatePrice();
            changeNumById(id, _num);
        })
    }

    function checkAllEvent() {
        $('#checkAll').tap(function () {
            var check = $(this).prop('checked');
            $('#shopCar .check-box').prop('checked', check);
        })
    }

    function isCheckAll() {
        var check = $('#shopCar .check-box').length;
        var checkedLen = $('#shopCar .check-box:checked').length;
        $('#checkAll').prop('checked', check === checkedLen);
    }

    function checkEvent() {
        $('#shopCar').on('click', '.check-box', function () {
            calculatePrice();
            isCheckAll()
        })
    }

    function calculatePrice() {
        var total = 0;
        $('#shopCar .item').each(function () {
            var _vm = $(this);
            var isCheck = _vm.find('input[type=checkbox]').prop('checked');
            if (isCheck) {
                var price = _vm.find('.current').text().replace('￥', '');
                var num = _vm.find('.num').val();
                total += price * num;
            }
        });
        $('#total').text('￥' + total);
    }

    function getShopCarData() {
        var option = {
            url: HOST + '/admin/shopCar.php',
            data: {
                getType: 'shopCar',
                user: LsyCookie.getItem('loginUserName')
            }
        };
        request(option).then(function (data) {
            createShopCarHtml(data);
            calculatePrice();
        })
    }

    function createShopCarHtml(data) {
        var str = '';
        data.forEach(function (item) {
            var _check = item.check == 1 ? 'checked' : '';
            str += '<div class="item" data-id="' + item.shop_id + '">'
                + '<label class="check">'
                + '<input class="check-box" type="checkbox" ' + _check + '>'
                + '<span></span>'
                + '</label>'
                + '<img src="' + item.img_src + '" alt="pic">'
                + '<div class="desc">'
                + '<h3>' + item.title + '</h3>'
                + '<div class="price">'
                + '<div class="current">￥' + item.price + '</div>'
                + '<div class="old">￥' + item.old_price + '</div>'
                + '</div>'
                + '<div class="operation">'
                + '<div class="num-box">'
                + '<button class="reduce">-</button>'
                + '<input value="' + item.num + '" class="num" type="text" id="num">'
                + '<button class="plus">+</button>'
                + '</div>'
                + '<input type="button" data-id="' + item.shop_id + '" value="删除" class="del">'
                + '</div>'
                + '</div>'
                + '</div>';
        })
        $('#shopCar').html(str);
    }

    function del() {
        $('#shopCar').on('click', '.del', function () {
            var option = {
                url: HOST + '/admin/shopCar.php',
                type: 'post',
                data: {
                    postType: 'del',
                    id: $(this).attr('data-id'),
                    user: LsyCookie.getItem('loginUserName')
                }
            }
            request(option).then(function (data) {
                console.log(data);
            });
            $(this).parents('.item').remove();
        });
    }

    function check() {
        $('#shopCar').on('click', '.check-box', function () {
            var _vm = $(this);
            var id = _vm.parents('.item').attr('data-id');
            var check = _vm.is(':checked') ? 1 : 0;
            Selected(id, check);
        });
    }

    function Selected(id, check) {
        var option = {
            url: HOST + '/admin/shopCar.php',
            type: 'post',
            data: {
                postType: 'commit',
                check: check,
                id: id,
                user: LsyCookie.getItem('loginUserName')
            }
        };
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            request(option).then(function (data) {
                console.log(data);
            });
        }, 300);
    }

    function pay() {
        $('#pay').tap(function () {
            checkLoginStatus();
            window.location.href = '../html/confirm_order.html';
        });
    }
});